
package monitortest;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author the Dope Factory
 */
public class LedManager implements Runnable{
    Thread process;
    
    public LedManager(){
        process=new Thread(this,"Gestione led");
        process.start();
    }
    public void control(){
        //SWITCH 1
        if(ManagementSerial.switch1Status==true){
            Interface.setSwitch1Visible(false, false, false, false, false, true);
        }else{
            Interface.setSwitch1Visible(true, false, false, false, false, false);
        }
        if(ManagementSerial.assorbimentoDetector<10){               //equivalente a 400 mAh per una soglia di inattività.
            Interface.setSwitch1Visible(false, false, false, false, true, false);
        }
        //SWITCH 2
        if(ManagementSerial.switch2Status==true){
            Interface.setSwitch2Visible(false, false, false, false, false, true);
        }else{
            Interface.setSwitch2Visible(true, false, false, false, false, false);
        }
        if(ManagementSerial.assorbimentoDetector<10){               //equivalente a 400 mAh per una soglia di inattività.
            Interface.setSwitch2Visible(false, false, false, false, true, false);
        }
        //DETECTOR
        if(ManagementSerial.assorbimentoDetector<ManagementSerial.sogliaDetector){
            Interface.setDetectorVisible(false, false, false, false, false, true);            
        }else{
            Interface.setDetectorVisible(true, false, false, false, false, false);
        }
        if(ManagementSerial.assorbimentoDetector<20){               //equivalente a 400 mAh per una soglia di inattività.
            Interface.setDetectorVisible(false, false, false, false, true, false);
        }
        //CONTROLLER
        if(ManagementSerial.assorbimentoDetector<ManagementSerial.sogliaDetector){
            Interface.setControllerVisible(false, false, false, false, false, true);            
        }else{
            Interface.setControllerVisible(true, false, false, false, false, false);
        }
    }
    
    public void measure(){
        double max= 5.625; //massima portata del sensore, equivalente a 5,6Kwh
        //calcolo consumo totale abitazione
        double consumoTotale=((ManagementSerial.assorbimentoDetector*25.0)/1023.0);      //25 A, 1023bit
        Interface.consTotT.setText(consumoTotale+" Ah");
        //calcolo consumo s1
        double consumoS1=(ManagementSerial.assorbimentoSwitch1*25.0)/1023.0;
        Interface.consS1T.setText(consumoS1+" Ah");
        //calcolo consumo s2
        double consumoS2=(ManagementSerial.assorbimentoSwitch2*25.0)/1023.0;
        Interface.consS2T.setText(consumoS2+" Ah");
        //soglia
        double soglia=(ManagementSerial.sogliaDetector*25.0)/1023.0;
        Interface.sogliaT.setText(soglia+" Ah");
        
        
//        if(consumoTotale>3.3){
//            double scarto=3.3-consumoTotale;
//        }
    }
    
    @Override
    public void run() {
        control();
        process.setPriority(Thread.NORM_PRIORITY);
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(LedManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        measure();
//        try {
//            Thread.sleep(100);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(LedManager.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
    
}
